﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NoteExpress;
namespace neut
{
    public partial class ItemInfo : Form
    {
        ListViewItemComparer lvwColumnSorter=new ListViewItemComparer(2, SortOrder.Ascending);
        public ItemInfo()
        {
            InitializeComponent();

            listView1.ListViewItemSorter = lvwColumnSorter; 
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var ne = new NoteExpress.NEHost2();
            var id = this.listView1.SelectedItems[0].Text;
            var t=ne.Libraries.ActiveFolder.FindItemFromID(id);
            bool rt;
            ne.Internals.EditReference(ne.Libraries.ActiveFolder.ParentLibrary.UID,t.UID,out rt);
        }

        private void outputToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var folder = Tag.ToNEFolder();
            var ne = new NoteExpress.NEHost2();
            foreach(ListViewItem t in this.listView1.SelectedItems)
            {
                ne.Libraries.ActiveFolder.LinkItem(folder.FindItemFromID(t.Text));                
            }
        }

        private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            // Determine if clicked column is already the column that is being sorted.
            if (e.Column == lvwColumnSorter.SortColumn)
            {
                // Reverse the current sort direction for this column.
                if (lvwColumnSorter.Order == SortOrder.Ascending)
                {
                    lvwColumnSorter.Order = SortOrder.Descending;
                }
                else
                {
                    lvwColumnSorter.Order = SortOrder.Ascending;
                }
            }
            else
            {
                // Set the column number that is to be sorted; default to ascending.
                lvwColumnSorter.SortColumn = e.Column;
                lvwColumnSorter.Order = SortOrder.Ascending;
            }

            // Perform the sort with these new sort options.
            this.listView1.Sort();
        }
            
    }


    class ListViewItemComparer : System.Collections.IComparer
    {
        public int SortColumn;
        public SortOrder Order;
        public ListViewItemComparer()
        {
            SortColumn = 0;
            Order = SortOrder.Ascending;
        }
        public ListViewItemComparer(int column, SortOrder order)
        {
            SortColumn = column;
            this.Order = order;
        }
        public int Compare(object x, object y)
        {
            int returnVal=0;
            switch (SortColumn)
            {
                case 0:
                case 1:
                    int kk1=0;
                    if (!int.TryParse(((ListViewItem)x).SubItems[SortColumn].Text, out kk1)) kk1 = 0;
                    var kk2 = 0;
                    if (!int.TryParse(((ListViewItem)y).SubItems[SortColumn].Text, out kk2)) kk2 = 0;
                    if (kk2 > kk1)
                        returnVal = (Order == SortOrder.Ascending ? -1:1);
                    else   if (kk2 < kk1)
                            returnVal = Order == SortOrder.Ascending ? 1:-1;
                   else
                            returnVal = 0;
                    break;
                case 2:
                    decimal w;
                    DateTime r;
                    bool k = Decimal.TryParse(((ListViewItem)x).SubItems[SortColumn].Text, out w);
                    bool s = DateTime.TryParse(((ListViewItem)x).SubItems[SortColumn].Text, out r);
                    if (k == true && Order != SortOrder.Descending)//数字正排序
                    {
                        Decimal k1 = Int32.Parse(((ListViewItem)x).SubItems[SortColumn].Text);
                        Decimal k2 = Int32.Parse(((ListViewItem)y).SubItems[SortColumn].Text);
                        if (k2 > k1)
                            returnVal = -1;
                        if (k2 < k1)
                            returnVal = 1;
                        else
                            returnVal = 0;
                    }
                    else
                    {
                        if (s == true)//时间排序
                        {
                            System.DateTime firstDate = DateTime.Parse(((ListViewItem)x).SubItems[SortColumn].Text);
                            System.DateTime secondDate = DateTime.Parse(((ListViewItem)y).SubItems[SortColumn].Text);
                            returnVal = DateTime.Compare(firstDate, secondDate);
                        }
                        else//字符排序
                        {
                            returnVal = String.Compare(((ListViewItem)x).SubItems[SortColumn].Text,
                                        ((ListViewItem)y).SubItems[SortColumn].Text);
                        }
                    }

                    if (Order == SortOrder.Descending)
                    {
                        if (k == true)//数字倒排序
                        {
                            System.Int32 k3 = Int32.Parse(((ListViewItem)x).SubItems[SortColumn].Text);
                            System.Int32 k4 = Int32.Parse(((ListViewItem)y).SubItems[SortColumn].Text);
                            if (k3 > k4)
                                returnVal = 1;
                            if (k3 < k4)
                                returnVal = -1;
                            else
                                returnVal = 0;
                        }
                        returnVal *= -1;
                    }
                    break;
            }
            return returnVal;
        }
    }
}
